﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using RoadTeam.Model;
using RoadTeam.Enums;
using RoadTeam.SysInterface;
using RoadTeam.Pub;

namespace RoadTeam.BLL
{
    
    public class CsValidateProBll
    {
        private static object o = new object();
        public DataTable GetCsValidateProData(String OrganIdentifier)
        {
            ReflashData(OrganIdentifier);
            SqlWhere where = new SqlWhere();
            where.Info = new CsValidatePro() { OrganIdentifier = OrganIdentifier };
           // where.SelectColumn = @" ValidateProIdentifier,ValidateProName,ValidateProCode,MaintenanceTime ";
            return CsValidatePro.Select(where, GlobalConfig.ConnectionString);
        }

        private void ReflashData(string OrganIdentifier)
        {
            String OrganID = FactoryClass.CreateSmOrgan().GetOraganID(OrganIdentifier);
            DataTable dt_Pro = GetEmptyTable();
            DataRow dr_Pro;
            lock (o)
            {
                DataTable dt = FactoryClass.CreateCsValidatePro().GetValidateProTable(OrganID);
                foreach (DataRow dr in dt.Rows)
                {
                    dr_Pro = dt_Pro.NewRow();
                    dr[3] = CreateIdentifier.GetIdentifier();
                    dr_Pro["ValidateProIdentifier"] = dr[3];
                    dr_Pro["ValidateProName"] = dr[0];
                    dr_Pro["ValidateProCode"] = dr[2];
                    dr_Pro["ValidateObjectIdentifier"] =FactoryClass.CreateCsValidateObject().GetValidateObjectIdentifier(dr[1].ToString());
                    dr_Pro["OrganIdentifier"] = FactoryClass.CreateSmOrgan().GetIdentitiferByID(dr[4].ToString()); 
                    dt_Pro.Rows.Add(dr_Pro);
                }
                FactoryClass.CreateCsValidatePro().UpdateValidateProTable(dt);
            }
            CsValidatePro.InsertDatatable(dt_Pro, GlobalConfig.ConnectionString);
        }


        public DataTable GetEmptyTable()
        {
            SqlWhere where = new SqlWhere();
            where.Info = new CsValidatePro() { ValidateProIdentifier=@"-1" };
            where.SelectColumn = @" ValidateProIdentifier,ValidateObjectIdentifier,ValidateProName,ValidateProCode,OrganIdentifier ";
            return CsValidatePro.Select(where, GlobalConfig.ConnectionString);
        }

    }
}
